package com.example.trader.service;


import com.example.trader.common.Result;
import com.example.trader.entity.MerchantPersonalInfo;
import com.example.trader.entity.TraderLogin;
import com.example.trader.mapper.TraderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TraderService {


    @Autowired
    private TraderMapper traderMapper;

    public Result updateTraderInfo(MerchantPersonalInfo traderInfo) {
        int count = traderMapper.updateTraderInfo(traderInfo);
        if (count == 0) {
            return Result.error("400", "修改失败");
        }
        return Result.success("修改成功");
    }


    public List<MerchantPersonalInfo> getTraderInfo() {

        List<MerchantPersonalInfo> trade = traderMapper.getTraderInfo();

        return trade;

    }

    public Result login(TraderLogin traderLogin) {

        TraderLogin trader = traderMapper.login(traderLogin);

        if(trader!=null){
            String username = trader.getUsername();
            String password = trader.getPassword();
            if (username != null || password != null) {
                return Result.success("登录成功");


            }
        }
        return Result.error("400", "登录失败");


    }

    public Result register(TraderLogin traderLogin) {

        TraderLogin trader = traderMapper.register(traderLogin);
        if(trader!=null){

            String username = trader.getUsername();
            String password = trader.getPassword();
            if(username!=null&&password!=null){
                return Result.error("400","该用户已经注册");
            }

        }

      int count=  traderMapper.insertTrader(traderLogin);
        if(count==0)
        {
            return Result.error("400","注册失败");
        }
        return Result.success("注册成功");

    }
}
